namespace YXShop.ExcelLite
{
    using System;
    using System.IO;
    using System.Text;

    internal class ExcelStringBase : BinaryWritable
    {
        public ExcelStringOptions Options;
        public string Str;

        public ExcelStringBase()
        {
        }

        public ExcelStringBase(string str)
        {
            this.Str = str;
            this.Options = ExcelStringOptions.Uncompressed;
        }

        public override bool Equals(object obj)
        {
            ExcelLongString str = (ExcelLongString) obj;
            return ((this.Str == str.Str) && (this.Options == str.Options));
        }

        public string GetFormattedStr()
        {
            StringBuilder builder = new StringBuilder(this.Str);
            for (int i = 0; i < builder.Length; i++)
            {
                char c = builder[i];
                if (!(char.IsLetterOrDigit(c) || (c == ' ')))
                {
                    builder[i] = 'X';
                }
            }
            return builder.ToString();
        }

        public override int GetHashCode()
        {
            return (this.Str.GetHashCode() ^ this.Options.GetHashCode());
        }

        protected void ReadOptionsAndString(BinaryReader br, ref int remainingSize, ref int charsRemaining)
        {
            int num;
            ushort num2;
            uint num3;
            this.Options = (ExcelStringOptions) br.ReadByte();
            remainingSize--;
            if (((byte) (this.Options & ExcelStringOptions.RichText)) != 0)
            {
                num2 = br.ReadUInt16();
                remainingSize -= 2;
            }
            else
            {
                num2 = 0;
            }
            if (((byte) (this.Options & ExcelStringOptions.AsianPhonetic)) != 0)
            {
                num3 = br.ReadUInt32();
                remainingSize -= 4;
            }
            else
            {
                num3 = 0;
            }
            char[] chArray = new char[charsRemaining];
            if (((byte) (this.Options & ExcelStringOptions.Uncompressed)) != 0)
            {
                for (num = 0; (charsRemaining > 0) && (remainingSize > 0); num++)
                {
                    chArray[num] = br.ReadChar();
                    remainingSize -= 2;
                    charsRemaining--;
                }
            }
            else
            {
                num = 0;
                while ((charsRemaining > 0) && (remainingSize > 0))
                {
                    chArray[num] = (char) br.ReadByte();
                    remainingSize--;
                    charsRemaining--;
                    num++;
                }
            }
            this.Str = new string(chArray, 0, chArray.Length - charsRemaining);
            for (num = 0; (num < num2) && (remainingSize > 0); num++)
            {
                br.ReadUInt32();
                remainingSize -= 4;
            }
            for (num = 0; (num < num3) && (remainingSize > 0); num++)
            {
                br.ReadByte();
                remainingSize--;
            }
        }

        public override void Write(BinaryWriter bw)
        {
            bw.Write((byte) this.Options);
            if (((byte) (this.Options & ExcelStringOptions.Uncompressed)) != 0)
            {
                foreach (char ch in this.Str)
                {
                    bw.Write(ch);
                }
            }
            else
            {
                foreach (char ch2 in this.Str)
                {
                    bw.Write((byte) ch2);
                }
            }
        }

        public override int Size
        {
            get
            {
                int num = 1;
                if (((byte) (this.Options & ExcelStringOptions.Uncompressed)) != 0)
                {
                    return (num + (this.Str.Length * 2));
                }
                return (num + this.Str.Length);
            }
        }
    }
}

